package com.example.webpractice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.webpractice.dto.KechengxinxiDTO;
import com.example.webpractice.po.KechengxinxiPO;
import com.example.webpractice.service.KechengxinxiService;
import com.example.webpractice.utils.ServerResponseEntity;
import com.example.webpractice.vo.KechengxinxiDetailVO;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Arrays;

import static com.example.webpractice.utils.ResponseEnum.internalFail;
import static com.example.webpractice.utils.ResponseEnum.kechengExsit;

/**
 * 课程信息
 * 后端接口
 */
@RestController
@RequestMapping("/kechengxinxi")
public class KechengxinxiController {
    @Autowired
    private KechengxinxiService kechengxinxiService;


    /**
     * 后端列表
     */
    @RequestMapping("/page")
    public ServerResponseEntity page(@RequestParam(name = "page") Integer page,
                                     @RequestParam(name = "limit") Integer limit,
                                     @RequestParam(name = "kechengmingcheng", required = false) String kechengmingcheng,
                                     @RequestParam(name = "sfsh", required = false) String sfsh,
                                     @RequestParam(name = "kechengfenlei", required = false) String kechengfenlei,
                                     @RequestParam(name = "jiaoshixingming", required = false) String jiaoshixingming,
                                     @RequestParam(name = "shangkedidian", required = false) String shangkedidian,
                                     HttpServletRequest request) throws IOException {
        QueryWrapper<KechengxinxiPO> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(kechengmingcheng)) {
            queryWrapper.like("kechengmingcheng", kechengmingcheng);
        }
        if (StringUtils.isNotBlank(sfsh)) {
            queryWrapper.eq("sfsh", sfsh);
        }
        if (StringUtils.isNotBlank(kechengfenlei)) {
            queryWrapper.like("kechengfenlei", kechengfenlei);
        }
        if (StringUtils.isNotBlank(jiaoshixingming)) {
            queryWrapper.like("jiaoshixingming", jiaoshixingming);
        }
        if (StringUtils.isNotBlank(shangkedidian)) {
            queryWrapper.like("shangkedidian", shangkedidian);
        }
        String tableName = (String) request.getAttribute("tableName");
        String username;
        if (tableName.equals("jiaoshi")) {
            username = (String) request.getAttribute("username");
            queryWrapper.eq("gonghao", username);
        }
        tableName = request.getAttribute("tableName").toString();
        if (tableName.equals("xuesheng")) {
            queryWrapper.eq("xuehao", request.getAttribute("username"));
        }
        return ServerResponseEntity.success(kechengxinxiService.queryPage(page, limit, queryWrapper));
    }

    /**
     * 详情
     */
    @RequestMapping("/info/{id}")
    public ServerResponseEntity info(@PathVariable("id") Long id) {
        KechengxinxiDetailVO vo = kechengxinxiService.selectById(id);
        return ServerResponseEntity.success(vo);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public ServerResponseEntity delete(@RequestBody Long[] ids) {
        if (kechengxinxiService.deleteBatchIds(Arrays.asList(ids)) != ids.length) {
            return ServerResponseEntity.fail(-1, "删除失败");
        }
        return ServerResponseEntity.success();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public ServerResponseEntity update(@RequestBody KechengxinxiDTO kechengxinxiDTO) {
        if (kechengxinxiService.update(kechengxinxiDTO) == 1) {
            return ServerResponseEntity.success();
        } else {
            return ServerResponseEntity.fail(-1, "更新失败");
        }
    }


    /**
     * 新增课程
     */
    @RequestMapping("/save")
    public ServerResponseEntity save(@RequestBody KechengxinxiDTO kechengxinxiDTO) {
        if (kechengxinxiService.selectByName(kechengxinxiDTO.getKechengmingcheng()) != null) {
            return ServerResponseEntity.fail(kechengExsit);
        } else {
            if (kechengxinxiService.create(kechengxinxiDTO) <= 0) {
                return ServerResponseEntity.fail(internalFail);
            }
            return ServerResponseEntity.success();
        }

    }
}
